/* $RCSFile: RssReader.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/RssReader/RssReader.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:22  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:59  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Settings;
using AddonNice.Diagnostics;
using AddonNice.Configuration.Settings;
using AddonNice.Settings.PageModules.Announcements;
using AddonNice.CRMWrapper;

using AddonNice.PageModules.Announcements; // for AStr

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.RssReader
{
    public class RStr
    {
        public const string RssSource   =   "RSSREADER_URL";
        public const string RssXPath    =   "RSSREADER_XPATH";
    }
    /// <summary>
    /// Initializer for RssReader 
    /// </summary>
    public class RssReaderSI : PortalModuleControlSI
    {
        public RssReaderSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]RssReaderSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]RssReaderSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }

        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]RssReaderSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            base.InitSettings(bSettings,pm);

            string extgrp                           =   pm.ExtendedResourcesGroup;
            
			// Set Editor Settings 
			HtmlEditorDataType.AddEditorToSettings(bSettings, SettingItemGroup.MODULE_SPECIAL_SETTINGS,extgrp);

            
            SettingItemSpecial RssSource	        =	new SettingItemSpecial(RStr.RssSource,new StringDataType());
			RssSource.Value		                    =	"http://blogs.telerik.com/Blogs.rss";        
			RssSource.Order		                    =	1;	
			RssSource.ExtendedGroupName             =   extgrp;
            RssSource.Description		            =	"Input the url where rss will be readen.";
			RssSource.EnglishName		            =	"Rss Source";
			bSettings.AddSetting(RssSource);

            SettingItemSpecial RssXPath	            =	new SettingItemSpecial(RStr.RssXPath,new StringDataType());
			RssXPath.Value		                    =	"rss/channel/item";        
			RssXPath.Order		                    =	5;	
			RssXPath.ExtendedGroupName              =   extgrp;
            RssXPath.Description		            =	"XPath selector from the rss feed to use.";
			RssXPath.EnglishName		            =	"Rss XPath";
			bSettings.AddSetting(RssXPath);
            /*
			//Custom settings
            SettingItemSpecial DelayExpire		    =	new SettingItemSpecial(AStr.DelayExpire,new IntegerDataType());
            DelayExpire.Value			            =	"60";
            DelayExpire.MinValue		            =	10;
            DelayExpire.MaxValue		            =	3650; //10 years
			DelayExpire.ExtendedGroupName           =    extgrp;
            DelayExpire.Description		            =	"Expiration delay";
			DelayExpire.EnglishName		            =	"Expiration delay";
            bSettings.AddSetting(DelayExpire);
            */

			SettingItemSpecial ScrollDirection      =	new SettingItemSpecial(AStr.ScrollDirection,new ListDataType("Down;Left;Right;Up"));
			ScrollDirection.Required	            =	true;
			ScrollDirection.Value		            =	"Up";
			ScrollDirection.Order		            =   20;	
			ScrollDirection.ExtendedGroupName       =    extgrp;
            ScrollDirection.Description		        =	"Specifies possible directions for scrolling rotator frames. This property can take as value any combination of the Up, Down, Left and Right values.";
			ScrollDirection.EnglishName		        =	"Scroll Direction";
			bSettings.AddSetting(ScrollDirection);

            /*
AutomaticAdvance - Items are scrolled automatically in the desired direction. Each item is shown for a specific amount of time. 
Buttons - For each scroll direction the rotator will display a control button. The user needs to click this button to scroll a new item. 
ButtonsOver - For each scroll direction the rotator will display a control button. The user needs to hover over this button with the mouse to scroll new items. 
SlideShow - Like automatic advance, but instead of scrolling, new items are displayed using a transition effect (e.g. Fade). 
SlideShowButtons - For each scroll direction the rotator will display a control button. The user needs to click this button to display a new item. 
FromCode - The rotator will not change items automatically and will not display the control buttons. The developer needs to use the rotator Client API to move items.
             */
            ArrayList arType                        =   new ArrayList();
            ListItem it                             =   new ListItem("AutomaticAdvance",Telerik.Web.UI.RotatorType.AutomaticAdvance.ToString());
            arType.Add(it);
            it                                      =   new ListItem("Buttons",Telerik.Web.UI.RotatorType.Buttons.ToString());
            arType.Add(it);
            it                                      =   new ListItem("ButtonsOver",Telerik.Web.UI.RotatorType.ButtonsOver.ToString());
            arType.Add(it);
            it                                      =   new ListItem("FromCode",Telerik.Web.UI.RotatorType.FromCode.ToString());
            arType.Add(it);
            it                                      =   new ListItem("SlideShow",Telerik.Web.UI.RotatorType.SlideShow.ToString());
            arType.Add(it);
            it                                      =   new ListItem("SlideShowButtons",Telerik.Web.UI.RotatorType.SlideShowButtons.ToString());
            arType.Add(it);
			SettingItemSpecial RotatorType          =	new SettingItemSpecial(AStr.RotatorType,new CustomListDataType(arType,"Text","Value"));
			RotatorType.Required	                =	true;
			RotatorType.Value		                =	Telerik.Web.UI.RotatorType.AutomaticAdvance.ToString();
			RotatorType.Order		                =	30;	
			RotatorType.ExtendedGroupName           =   extgrp;
            RotatorType.Description		            =	"Specifies the type of rotator [how the rotator will render and what options the user will have for interacting with it on the client]. Possible values for this property are: AutomaticAdvance, Buttons, FromCode.";
                
			RotatorType.EnglishName		            =	"Rotator Type";
			bSettings.AddSetting(RotatorType);
			
            ArrayList arAnType                      =   new ArrayList();
            foreach(string animationType in Enum.GetNames(typeof(Telerik.Web.UI.Rotator.AnimationType)))
                arAnType.Add(new ListItem(animationType,animationType));
			SettingItemSpecial AnimationType        =	new SettingItemSpecial(AStr.AnimationType,new CustomListDataType(arAnType,"Text","Value"));
			AnimationType.Required	                =	true;
			AnimationType.Value		                =	Telerik.Web.UI.AnimationType.None.ToString();
			AnimationType.Order		                =	40;	
			AnimationType.ExtendedGroupName         =   extgrp;
            AnimationType.Description		        =	"Specifies the type of animation. The SlideShowAnimation-Type takes effect only when Rotator Type property is set to SlideShow or SlideShowButtons.";
			AnimationType.EnglishName		        =	"Animation Type";
			bSettings.AddSetting(AnimationType);
			
            IntegerDataType idt                     =   new IntegerDataType();
            idt.DecimalDigits                       =   0;
			SettingItemSpecial ScrollDuration	    =	new SettingItemSpecial(AStr.ScrollDuration,idt);
			ScrollDuration.Value		            =	"2000";        
			ScrollDuration.Order		            =	50;	
			ScrollDuration.ExtendedGroupName        =   extgrp;
            ScrollDuration.Description		        =	"Specifies the speed in milliseconds for scrolling rotator frames.";
			ScrollDuration.EnglishName		        =	"Scroll duration";
			bSettings.AddSetting(ScrollDuration);

            idt                                     =   new IntegerDataType();
            idt.DecimalDigits                       =   0;
			SettingItemSpecial FrameDuration	    =	new SettingItemSpecial(AStr.FrameDuration,idt);
			FrameDuration.Value		                =	"1";        
			FrameDuration.Order		                =	60;	
			FrameDuration.ExtendedGroupName         =    extgrp;
            FrameDuration.Description		        =	"Specifies the speed in milliseconds for frames presentation.";
			FrameDuration.EnglishName		        =	"Frame duration";
			bSettings.AddSetting(FrameDuration);

			SettingItemSpecial TextWidth	        =	new SettingItemSpecial(AStr.TextWidth,new UnitDataType());
			TextWidth.Value		                    =	"300px";        
			TextWidth.Order		                    =	70;	
			TextWidth.ExtendedGroupName             =   extgrp;
            TextWidth.Description		            =	"Text Width.";
			TextWidth.EnglishName		            =	"Text Width";
			bSettings.AddSetting(TextWidth);

			SettingItemSpecial TextHeight	        =	new SettingItemSpecial(AStr.TextHeight,new UnitDataType());
			TextHeight.Value		                =	"120px";        
			TextHeight.Order		                =	80;	
			TextHeight.ExtendedGroupName            =   extgrp;
            TextHeight.Description		            =	"Text Height.";
			TextHeight.EnglishName		            =	"Text Height";
			bSettings.AddSetting(TextHeight);

			SettingItemSpecial ItemWidth	        =	new SettingItemSpecial(AStr.ItemWidth,new UnitDataType());
			ItemWidth.Value		                    =	"100px";        
			ItemWidth.Order		                    =	90;	
			ItemWidth.ExtendedGroupName             =   extgrp;
            ItemWidth.Description		            =	"Item Width.";
			ItemWidth.EnglishName		            =	"Item Width";
			bSettings.AddSetting(ItemWidth);

			SettingItemSpecial ItemHeight	        =	new SettingItemSpecial(AStr.ItemHeight,new UnitDataType());
			ItemHeight.Value		                =	"118px";        
			ItemHeight.Order		                =	100;	
			ItemHeight.ExtendedGroupName            =   extgrp;
            ItemHeight.Description		            =	"Item Height.";
			ItemHeight.EnglishName		            =	"Item Height";
			bSettings.AddSetting(ItemHeight);

            SettingItemSpecial WrapFrames	        =	new SettingItemSpecial(AStr.WrapFrames,new BooleanDataType());
			WrapFrames.Value		                =	StStr.strTrue;        
			WrapFrames.Order		                =	110;	
			WrapFrames.ExtendedGroupName            =   extgrp;
            WrapFrames.Description		            =	"When set to true, this property configures the RadRotator to start scrolling the rotator frames from the beginning, once the rotator shows the last frame.";
			WrapFrames.EnglishName		            =	"Wrap Frames";
			bSettings.AddSetting(WrapFrames);

            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]RssReaderInitializer Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
    }

    /// <summary>
    /// Rotating announcements which could take items from db or from CRM only
    /// </summary>
    public partial class RssReader : PortalModuleControl 
    {

		#region General Implementation
		public override Guid GuidID 
		{
			get
			{
				return new Guid(StStr.RssReaderGuid);
			}
		}

		#endregion

        /// <summary>
        /// Public constructor. Sets base settings for module.
        /// </summary>
        public RssReader() 
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]RssReader Ctor END.",ModuleTraceSwitch.Sw.Info);
        }

        public override string ExtendedResourcesGroup 
		{
            get
            {
       			const string extgrp                     =   "RSSREADER";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new RssReaderSI(bSettings,this);
        }


        /// <summary>
        /// The Page_Load event handler on this User Control is used to
        /// obtain a DataSet of announcement information from the RssReader
        /// table, and then databind the results to a templated DataList
        /// server control.  It uses the AddonNice.RssReaderDB()
        /// data component to encapsulate all data functionality. 
        /// </summary>
        protected void Page_Load(object sender, System.EventArgs e) 
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RssReader Page_Load Time: {0}, IsPostBack: {1}",DateTime.UtcNow,IsPostBack),ModuleTraceSwitch.Sw.Info);
            if ( !IsPostBack )
            {
                ConfigureRotator();		
	            xmlDataSource1.DataFile =   Settings[RStr.RssSource].ToString();
                xmlDataSource1.XPath    =   Settings[RStr.RssXPath].ToString();
			    if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]RssReader Page_Load DataFile: {0}, XPath: {1}",xmlDataSource1.DataFile,xmlDataSource1.XPath),ModuleTraceSwitch.Sw.Info);
            }
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("RssReader Page_Load END.",ModuleTraceSwitch.Sw.Info);
        }


        protected void ConfigureRotator()
        {
		    string sDirection    =   Settings[AStr.ScrollDirection].ToString();
            switch ( sDirection )
            {
                default:
                case "Down":
                myRotator.ScrollDirection = Telerik.Web.UI.RotatorScrollDirection.Down;
                break;
                case "Left":
                myRotator.ScrollDirection = Telerik.Web.UI.RotatorScrollDirection.Left;
                break;
                case "Right":
                myRotator.ScrollDirection = Telerik.Web.UI.RotatorScrollDirection.Right;
                break;
                case "Up":
                myRotator.ScrollDirection = Telerik.Web.UI.RotatorScrollDirection.Up;
                break;
            }
            
		    string rType    =   Settings[AStr.RotatorType].ToString();
            switch ( rType )
            {
                default:
                case "AutomaticAdvance":
                myRotator.RotatorType =  Telerik.Web.UI.RotatorType.AutomaticAdvance;
                break;
                case "Buttons":
                myRotator.RotatorType =  Telerik.Web.UI.RotatorType.Buttons;
                break;
                case "ButtonsOver":
                myRotator.RotatorType =  Telerik.Web.UI.RotatorType.ButtonsOver;
                break;
                case "FromCode":
                myRotator.RotatorType =  Telerik.Web.UI.RotatorType.FromCode;
                break;
                case "SlideShow":
                myRotator.RotatorType =  Telerik.Web.UI.RotatorType.SlideShow;
                break;
                case "SlideShowButtons":
                myRotator.RotatorType =  Telerik.Web.UI.RotatorType.SlideShowButtons;
                break;
            }

		    myRotator.SlideShowAnimation.Type =   (Telerik.Web.UI.Rotator.AnimationType)Enum.Parse(typeof(Telerik.Web.UI.Rotator.AnimationType), Settings[AStr.AnimationType].ToString());


            myRotator.Width                 =   Settings[AStr.TextWidth].ToUnit();
            myRotator.Height                =   Settings[AStr.TextHeight].ToUnit();

            //set item size
            myRotator.ItemWidth             =   Settings[AStr.ItemWidth].ToUnit();
            myRotator.ItemHeight            =   Settings[AStr.ItemHeight].ToUnit();

            if ( myRotator.RotatorType == RotatorType.ButtonsOver || myRotator.RotatorType == RotatorType.Buttons ||
               myRotator.RotatorType == RotatorType.SlideShowButtons )
            {
                if ( ( myRotator.ScrollDirection == RotatorScrollDirection.Left ) || ( myRotator.ScrollDirection == RotatorScrollDirection.Right))
                         myRotator.ScrollDirection  =   RotatorScrollDirection.Left | RotatorScrollDirection.Right;
                if ( ( myRotator.ScrollDirection == RotatorScrollDirection.Up ) || ( myRotator.ScrollDirection == RotatorScrollDirection.Down))
                    myRotator.ScrollDirection =     RotatorScrollDirection.Down | RotatorScrollDirection.Up;
                //if rotator type is ButtonsOver (i.e. scrolling should be continuous) we set the frame duration to 0
                myRotator.FrameDuration = 1;
            }
            else
            {
                myRotator.ScrollDuration    =   Settings[AStr.FrameDuration].ToInt();
            }

            myRotator.ScrollDuration    =   Settings[AStr.ScrollDuration].ToInt();
            myRotator.WrapFrames        =   Settings[AStr.WrapFrames].ToBool();
        }


        #region Std Overrides


        override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

        private void InitializeComponent() 
        {
			Load                        +=  new EventHandler(Page_Load);
		}

		#endregion
    }
}

